/**
 * 
 */
package com.wsd.action.admin;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.BeanUtils;

import com.wsd.action.BaseAction;
import com.wsd.bean.Admin;
import com.wsd.bean.Articles;
import com.wsd.service.ArticleService;
import com.wsd.util.SysUtil;
/**
 * @author Administrator
 *
 */
public class ArticleAction extends BaseAction {
	private ArticleService articleService;
	private Articles article;
	private int start;
	private int limit;
	private int total;
	private String text;
	private Date startDate;
	private Date endDate;
	private int[] ids;
	@JSON(serialize = false)
	public ArticleService getArticleService() {
		return articleService;
	}
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}
	@JSON(serialize = false)
	public Articles getArticle() {
		return article;
	}
	public void setArticle(Articles article) {
		this.article = article;
	}
	@JSON(serialize = false)
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	@JSON(serialize = false)
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	@JSON(serialize = false)
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	@JSON(serialize = false)
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	@JSON(serialize = false)
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	@JSON(serialize = false)
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	@JSON(serialize = false)
	public int[] getIds() {
		return ids;
	}
	public void setIds(int[] ids) {
		this.ids = ids;
	}
	/**
	 * 发布文章
	 * @return
	 */
	public String post(){
		article.setAdmin(SysUtil.currentAdmin());
		article.setPostTime(new Timestamp(new Date().getTime()));
		articleService.save(article);
		return AJAX;
	}
	
	public String history(){
		List<Articles> articles = articleService.history(startDate,endDate,start,limit);
		total = articleService.historyCount(startDate,endDate);
		if(articles != null){
			process(articles);
		}
		result.put("articles", articles);
		return AJAX;
	}
	
	public String delete(){
		articleService.delete(ids);
		return AJAX;
	}
	
	public String update(){
		Articles preArticle = articleService.findById(article.getAId());
		BeanUtils.copyProperties(article, preArticle, new String[]{"postTime","admin"});
		articleService.update(preArticle);
		return AJAX;
	}
	@JSON(serialize = false)
	public String getArticles(){
		List<Articles> articles = articleService.getArticles(article,start,limit);
		total = articleService.getArticlesCount(article);
		if(articles != null){
			process(articles);
		}
		result.put("articles", articles);
		return AJAX;
	}
	
	/**
	 * @param articles
	 */
	private void process(List<Articles> articles) {
		for(Articles article : articles){
			Admin admin = new Admin();
			BeanUtils.copyProperties(article.getAdmin(), admin, new String[]{"messages","articleses","productOrders"});
			article.setAdmin(admin);
		}
	}
	
}
